package com.example.springBoot.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.springBoot.common.BaseController;
import com.example.springBoot.dao.UserDao;
import com.example.springBoot.domain.User;

@Controller
public class UserController extends BaseController {

	@Autowired
	private UserDao userDao;

	@RequestMapping("/admin/findalluser")
	public String findall(Map<String, Object> map,
			@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC) Pageable pageable) {
		Page<User> userlist = userDao.findAll(pageable);
		map.put("userlist", userlist);
		return "admin/user/list";
	}

	@RequestMapping("/admin/adduser")
	public String adduser(User user) {
		user = new User();
		return "admin/user/add";
	}

	@RequestMapping("/admin/saveuser")
	public String saveuser(User user, Map<String, Object> map) {
		if (user.getId() != null) {
			User mysqluser = userDao.findByid(user.getId());
			mysqluser.setNickName(user.getNickName());
			mysqluser.setArea(user.getArea());
			mysqluser.setEmail(user.getEmail());
			mysqluser.setOccupation(user.getOccupation());
			mysqluser.setPhone(user.getPhone());
			mysqluser.setUserAge(user.getUserAge());
			mysqluser.setUserImg(user.getUserImg());
			mysqluser.setUserName(user.getUserName());
			mysqluser.setUserIntroduce(user.getUserIntroduce());
			mysqluser.setUserMotto(user.getUserMotto());
			userDao.save(mysqluser);
		} else {
			User checkuser = userDao.findByUserName(user.getUserName());
			if (checkuser == null) {
				Date date = new Date();
//				DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
//				String formattedDate = dateFormat.format(date);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
				String formatdate = sdf.format(date);
				User mysqluser = new User();
				mysqluser.setNickName(user.getNickName());
				mysqluser.setArea(user.getArea());
				mysqluser.setEmail(user.getEmail());
				mysqluser.setOccupation(user.getOccupation());
				mysqluser.setPassWord(user.getPassWord());
				mysqluser.setPhone(user.getPhone());
				mysqluser.setRegTime(formatdate);
				mysqluser.setUserAge(user.getUserAge());
				mysqluser.setUserImg(user.getUserImg());
				mysqluser.setUserName(user.getUserName());
				mysqluser.setUserIntroduce(user.getUserIntroduce());
				mysqluser.setUserMotto(user.getUserMotto());
				userDao.save(mysqluser);
			} else {
				String msg = "该用户名不可用！！！";
				map.put("errormsg", msg);
				return "admin/user/add";
			}
		}
		return "redirect:/admin/findalluser";
	}

	@RequestMapping("/admin/updateuser/{id}")
	public String updateuser(@PathVariable Long id, Map<String, Object> map) {
		User user = userDao.findByid(id);
		map.put("user", user);
		return "admin/user/update";
	}

	@RequestMapping("/admin/deluser/{id}")
	public String deluser(@PathVariable Long id, Map<String, Object> map,
			@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC) Pageable pageable) {
		userDao.delete(userDao.findByid(id));
		return "redirect:/admin/findalluser";
	}

	@RequestMapping("/checklogin")
	public String checklogin(String username, String password, Map<String, Object> map, HttpSession session) {
		User user = userDao.findByUserNameAndPassWord(username, password);
		if (user != null) {
			session.setAttribute("username", user.getNickName());
			session.setAttribute("userId", user.getId());
			return "redirect:/admin";
		} else {
			map.put("message", "您输入的用户名或者密码错误！！！");
			return "admin/login";
		}
	}

}
